/** 
  ******************************************************************************
  * @file    main.c
  * @author  SJU
  * @version V1.0
  * @date    2025-08
  * @brief   测试 KEY1和KEY2按键中断，K1实现计数加1，K2实现计数减1，并在数码管显示
 ******************************************************************************
  * @attention
  * 实验平台:SJU积木式单片机学习套件 
 ******************************************************************************
 **/

#include "stm32f10x.h"
#include "bsp_4led.h"
#include "bsp_exti.h"
#include "bsp_DigitalTube.h"
#include "Systick_Delay.h"
void Delay(__IO uint32_t nCount)           //简单的延时函数
{
	for(; nCount !=0;nCount--);
}

uint8_t KEY1_pressed=0;
void KEY_SCAN_FLAG(void)
{
	static uint8_t key_old=1;
	uint8_t key_now;
	key_now=GetKEY1();
	if(key_old==1 && key_now==0)
		KEY1_pressed=1;
		key_old=key_now;
}

uint8_t KEY2_pressed=0;
void KEY_SCAN_FLAG2(void)
{
	static uint8_t key_old=1;
	uint8_t key_now;
	key_now=GetKEY2();
	if(key_old==1 && key_now==0)
		KEY2_pressed=1;
		key_old=key_now;
}

uint8_t KEY_Micro_pressed=0;
void KEY_SCAN_FLAG3(void)
{
	static uint8_t key_old=1;
	uint8_t key_now;
	key_now=GetKEY_Micro();
	if(key_old==1 && key_now==0)
		KEY_Micro_pressed=1;
		key_old=key_now;
}

	
uint16_t LED_FLGY;

int main(void)
{	
	uint8_t Cnt=0;
	uint8_t Cnt1=0;
	/* 初始化 */
	LED_GPIO_Config();	
	/* 初始化EXTI中断*/
 	EXTI_Key_Config();	
  LED_RED_OFF();
	
	DigitalTube_GPIO_Config();
	
	
/* 等待中断，当按键有动作，程序会跳转到stm32f10x_it.c的相应程序中
  KEY1动作，进入程序EXTI9_5_IRQHandler()中（L437）
  KEY2动作，进入程序EXTI4_IRQHandler()中（L267）	*/	
	while (1)
	{
		LED_FLGY++;
		if(LED_FLGY<600)LED_RED_ON();
		else if(LED_FLGY<1000)LED_RED_OFF();
		else if(LED_FLGY<1600)LED_GREEN_ON();
		else if(LED_FLGY<2000)LED_GREEN_OFF();
		else if(LED_FLGY<2600)LED_BLUE_ON();
		else if(LED_FLGY<3000)LED_BLUE_OFF();
		else LED_FLGY=0;
		
		KEY_SCAN_FLAG2();
		if(KEY2_pressed)
		{
			KEY2_pressed=0;
			if(++Cnt>9) Cnt=0;
			LED_WHITE_ON();
			Delay(0x1fffff);
			LED_WHITE_OFF();
		}
		
		KEY_SCAN_FLAG();
		if(KEY1_pressed)
		{
			KEY1_pressed=0;
			if(--Cnt>98) Cnt=9;
			LED_WHITE_ON();
			Delay(0x1fffff);
			LED_WHITE_OFF();
		}
		shiftOut(digitPatterns[Cnt]);
		
		KEY_SCAN_FLAG3();
		if(KEY_Micro_pressed==1)
		{
			KEY_Micro_pressed=0;
			Cnt1=!Cnt1;
			if(Cnt1==1)
				{
					shiftOut(0x10);
					LED_BLUE_OFF();
	        LED_RED_OFF();
	        LED_GREEN_OFF();
	        LED_WHITE_OFF();
					Delay_Nms(1000);
				}
		}
		Delay_Nms(1);
	}
	
}
/*********************************************END OF FILE**********************/


